package my.fuelwallet.test;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FuelPriceHandle {
	private SQLiteDatabase db;
	private FuelPriceSQLiteHelper dbHelper;
	
	public FuelPriceHandle(Context context){
		dbHelper = new FuelPriceSQLiteHelper(context);
	}
	
	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	public boolean putFuelPrice(FuelPrice in_fuelprice){
		boolean NameAvailable = true;
		List<String> NameList = this.getFuelNameList();
		
		if(NameList.indexOf(in_fuelprice.getfuelName())!=-1){
			NameAvailable = false;
		}else{
			db.execSQL("INSERT INTO " + FuelPriceSQLiteHelper.TABLE_FUELPRICES + " (" 
				+ FuelPriceSQLiteHelper.COLUMN_FUELNAME + ","  
				+ FuelPriceSQLiteHelper.COLUMN_FUELPRICE + ")"
				+ " VALUES ("
				+ "\"" + in_fuelprice.getfuelName()+ "\"," 
				+ in_fuelprice.getfuelPrice() +")" ); 
		}
		return NameAvailable;
	}
	
	public void deleteFuelPrice(final String fuel_name){
		db.execSQL("DELETE FROM " + FuelPriceSQLiteHelper.TABLE_FUELPRICES
				+ " WHERE " + FuelPriceSQLiteHelper.COLUMN_FUELNAME + " = \"" + fuel_name+"\"");
	}
	
	public FuelPrice getFuelPrice(final String fuel_name){
		FuelPrice out_fuelPrice;
		float fuel_price;
		
		String args[] = { };
		
		Cursor result = db.rawQuery("SELECT " + FuelPriceSQLiteHelper.COLUMN_FUELPRICE
				+ " FROM "+FuelPriceSQLiteHelper.TABLE_FUELPRICES 
				+ " WHERE " + FuelPriceSQLiteHelper.COLUMN_FUELNAME + " = \""+fuel_name+"\"", args);
		
		result.moveToFirst();
		fuel_price = result.getFloat(0);
		
		out_fuelPrice=new FuelPrice(fuel_name, fuel_price);	
		
		result.close();
		return out_fuelPrice;
	}
	
	public List<String> getFuelNameList(){
		List<String> NameList = new ArrayList<String>();
		String[] args = { };
		
		Cursor result = db.rawQuery("SELECT " + FuelPriceSQLiteHelper.COLUMN_FUELNAME 
				+ " FROM " + FuelPriceSQLiteHelper.TABLE_FUELPRICES, args);
		
		result.moveToFirst();
		while (!result.isAfterLast()) {
			NameList.add(result.getString(0));
			result.moveToNext();
		}
		
		result.close();
		return NameList;
	}
	
	public class FuelPriceSQLiteHelper extends SQLiteOpenHelper {

		public static final String TABLE_FUELPRICES = "fuelprices";
		public static final String COLUMN_ID = "_id";
		public static final String COLUMN_FUELNAME = "fuel_name";
		public static final String COLUMN_FUELPRICE = "fuel_price";

		private static final String DATABASE_NAME = "fuelprices.db";
		private static final int DATABASE_VERSION = 1;

		// Database creation SQL statement
		private static final String DATABASE_CREATE = "create table "
				+ TABLE_FUELPRICES + "( " 
				+ COLUMN_ID + " integer primary key autoincrement, " 
				+ COLUMN_FUELNAME + " text not null, "
				+ COLUMN_FUELPRICE + " real not null);";

		public FuelPriceSQLiteHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase database) {
			database.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(FuelPriceSQLiteHelper.class.getName(),
					"Upgrading database from version " + oldVersion + " to "
							+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS" + TABLE_FUELPRICES);
			onCreate(db);
		}
	}
	

}